package cn.com.headfree.expert.dao.impl;

import cn.com.headfree.entity.TDeclareExpertInfo;
import cn.com.headfree.entity.TDeclareExpertPoint;
import cn.com.headfree.entity.TPersonYearPointDetail;
import cn.com.headfree.entity.TSalaryInfo;
import cn.com.headfree.entity.isc.User;
import cn.com.headfree.expert.dao.IPointManageDao;
import cn.com.headfree.vo.TDeclareExpertPointVo;
import com.gta.edu.sdk.base.dao.impl.BaseDaoImpl;
import com.gta.edu.sdk.common.dto.Page;
import com.gta.edu.sdk.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: TODO
 * @Author wcSun
 * @Date 2022/6/28
 **/
@Repository("pointManageDao")
public class PointManageDaoImpl extends BaseDaoImpl<TDeclareExpertInfo> implements IPointManageDao {

    @Override
    public void findDeclareExpertPointPageList(Page<TDeclareExpertPointVo> page, TDeclareExpertPointVo declareExpertPointVo) {
        ArrayList<Object> arrayList = new ArrayList<Object>();
        String sql = "select t.*,s.audit_status from t_declare_expert_point t " ;
        StringBuilder sqlBuilder = new StringBuilder(sql);
        sqlBuilder.append(" LEFT JOIN t_declare_expert_info s on s.id = t.declare_id and s.del_sign = 0 ");
        sqlBuilder.append(" where t.del_sign = 0  ");
        sqlBuilder.append(" AND t.org_id=? ");
        arrayList.add(declareExpertPointVo.getOrgId());
        if (declareExpertPointVo.getYear() != null) {
            sqlBuilder.append(" AND t.year= ? ");
            arrayList.add(declareExpertPointVo.getYear());
        }
        if(!StringUtil.isNullOrEmpty(declareExpertPointVo.getUserName())){
            sqlBuilder.append(" AND t.user_name LIKE concat('%',?,'%') ");
            arrayList.add(declareExpertPointVo.getUserName());
        }
        if(!StringUtil.isNullOrEmpty(declareExpertPointVo.getErpNo())){
            sqlBuilder.append(" AND t.erp_no =? ");
            arrayList.add(declareExpertPointVo.getErpNo());
        }
        if(!StringUtil.isNullOrEmpty(declareExpertPointVo.getDeptId())){
            sqlBuilder.append(" AND t.dept_id =? ");
            arrayList.add(declareExpertPointVo.getDeptId());
        }
        if(!StringUtil.isNullOrEmpty(declareExpertPointVo.getAuditStatus())){
            sqlBuilder.append(" AND s.audit_status =? ");
            arrayList.add(declareExpertPointVo.getAuditStatus());
        }
        sqlBuilder.append(" order by t.create_time desc ");
        this.findPageBySql(TDeclareExpertPointVo.class, sqlBuilder.toString(), page, arrayList.toArray());
    }

    @Override
    public List<TDeclareExpertPointVo> findDeclareExpertPointList(TDeclareExpertPointVo declareExpertPointVo) {
        ArrayList<Object> arrayList = new ArrayList<Object>();
        String sql = "select t.*,s.audit_status from t_declare_expert_point t " ;
        StringBuilder sqlBuilder = new StringBuilder(sql);
        sqlBuilder.append(" LEFT JOIN t_declare_expert_info s on s.id = t.declare_id and s.del_sign = 0 ");
        sqlBuilder.append(" where t.del_sign = 0  ");
        sqlBuilder.append(" AND t.org_id=? ");
        arrayList.add(declareExpertPointVo.getOrgId());
        if (declareExpertPointVo.getYear() != null) {
            sqlBuilder.append(" AND t.year= ? ");
            arrayList.add(declareExpertPointVo.getYear());
        }
        if(!StringUtil.isNullOrEmpty(declareExpertPointVo.getUserName())){
            sqlBuilder.append(" AND t.user_name LIKE concat('%',?,'%') ");
            arrayList.add(declareExpertPointVo.getUserName());
        }
        if(!StringUtil.isNullOrEmpty(declareExpertPointVo.getErpNo())){
            sqlBuilder.append(" AND t.erp_no =? ");
            arrayList.add(declareExpertPointVo.getErpNo());
        }
        if(!StringUtil.isNullOrEmpty(declareExpertPointVo.getDeptId())){
            sqlBuilder.append(" AND t.dept_id =? ");
            arrayList.add(declareExpertPointVo.getDeptId());
        }
        if(!StringUtil.isNullOrEmpty(declareExpertPointVo.getAuditStatus())){
            sqlBuilder.append(" AND s.audit_status =? ");
            arrayList.add(declareExpertPointVo.getAuditStatus());
        }
        sqlBuilder.append(" order by t.create_time desc ");
        return this.findAllBySQL(TDeclareExpertPointVo.class, sqlBuilder.toString(),null,false,arrayList.toArray());
    }

    @Override
    public void saveInfo(TDeclareExpertPoint declareExpertPoint) {
        this.save(declareExpertPoint);
    }

    @Override
    public void updateExpertPoint(TDeclareExpertPoint declareExpertPoint) {
        String sql = "update t_declare_expert_point set ";
        StringBuilder sqlBuilder = new StringBuilder(sql);
        Map<String, Object> params = new HashMap<String, Object>();
        if(declareExpertPoint.getYearEffectPoint()!=null){
            sqlBuilder.append(" year_effect_point=#{year_effect_point,jdbcType=DOUBLE},");
            params.put("year_effect_point", declareExpertPoint.getYearEffectPoint());
        }
        if(declareExpertPoint.getContiStudyPoint()!=null){
            sqlBuilder.append(" conti_study_point=#{conti_study_point,jdbcType=DOUBLE},");
            params.put("conti_study_point", declareExpertPoint.getContiStudyPoint());
        }
        if(declareExpertPoint.getSdtTutorPrentPoint()!=null){
            sqlBuilder.append(" sdt_tutor_prent_point=#{sdt_tutor_prent_point,jdbcType=DOUBLE},");
            params.put("sdt_tutor_prent_point", declareExpertPoint.getSdtTutorPrentPoint());
        }
        if(declareExpertPoint.getHonorPoint()!=null){
            sqlBuilder.append(" honor_point=#{honor_point,jdbcType=DOUBLE},");
            params.put("honor_point", declareExpertPoint.getHonorPoint());
        }
        if(declareExpertPoint.getTecManPoint()!=null){
            sqlBuilder.append(" tec_man_point=#{tec_man_point,jdbcType=DOUBLE},");
            params.put("tec_man_point", declareExpertPoint.getTecManPoint());
        }
        if(declareExpertPoint.getTecInnovPoint()!=null){
            sqlBuilder.append(" tec_innov_point=#{tec_innov_point,jdbcType=DOUBLE},");
            params.put("tec_innov_point", declareExpertPoint.getTecInnovPoint());
        }
        if(declareExpertPoint.getPatentPoint()!=null){
            sqlBuilder.append(" patent_point=#{patent_point,jdbcType=DOUBLE},");
            params.put("patent_point", declareExpertPoint.getPatentPoint());
        }
        if(declareExpertPoint.getAcadePoint()!=null){
            sqlBuilder.append(" acade_point=#{acade_point,jdbcType=DOUBLE},");
            params.put("acade_point", declareExpertPoint.getAcadePoint());
        }
        if(declareExpertPoint.getStandPoint()!=null){
            sqlBuilder.append(" stand_point=#{stand_point,jdbcType=DOUBLE},");
            params.put("stand_point", declareExpertPoint.getStandPoint());
        }
        if(declareExpertPoint.getTecStandardPoint()!=null){
            sqlBuilder.append(" tec_standard_point=#{tec_standard_point,jdbcType=DOUBLE},");
            params.put("tec_standard_point", declareExpertPoint.getTecStandardPoint());
        }
        if(declareExpertPoint.getExamPoint()!=null){
            sqlBuilder .append(" exam_point=#{exam_point,jdbcType=DOUBLE},");
            params.put("exam_point", declareExpertPoint.getExamPoint());
        }
        if(declareExpertPoint.getInterviewPoint()!=null){
            sqlBuilder.append(" interview_point=#{interview_point,jdbcType=DOUBLE},");
            params.put("interview_point", declareExpertPoint.getInterviewPoint());
        }
        if(declareExpertPoint.getTotalPoint()!=null){
            sqlBuilder.append(" total_point=#{total_point,jdbcType=DOUBLE},");
            params.put("total_point", declareExpertPoint.getTotalPoint());
        }
        if(StringUtils.isNotEmpty(declareExpertPoint.getTrimReason())){
            sqlBuilder.append(" trim_reason=#{trim_reason,jdbcType=VARCHAR},");
            params.put("trim_reason", declareExpertPoint.getTrimReason());
        }
        sqlBuilder.append(" updater_name=#{updater_name,jdbcType=VARCHAR},")
                .append(" update_time=#{update_time,jdbcType=DATE} ");
        sqlBuilder.append(" where ID=#{id,jdbcType=VARCHAR}");
        params.put("updater_name", declareExpertPoint.getUpdaterName());
        params.put("update_time", declareExpertPoint.getUpdateTime());
        params.put("id", declareExpertPoint.getId());
        this.executeSql(sqlBuilder.toString(), params);

    }

    @Override
    public TDeclareExpertPointVo getDeclareExpertPoint(String year, String orgId, String erpNo) {
        ArrayList<Object> arrayList = new ArrayList<Object>();
        String sql = "select t.*,s.audit_status from t_declare_expert_point t " ;
        StringBuilder sqlBuilder = new StringBuilder(sql);
        sqlBuilder.append(" LEFT JOIN t_declare_expert_info s on s.id = t.declare_id and s.del_sign = 0 ");
        sqlBuilder.append(" where t.del_sign = 0 ");
        sqlBuilder.append(" AND t.year=? ");
        sqlBuilder.append(" AND t.org_id=? ");
        sqlBuilder.append(" AND t.erp_no=? ");
        if (!StringUtil.isNullOrEmpty(year)) {
            arrayList.add(year);
        }
        if(!StringUtil.isNullOrEmpty(orgId)){
            arrayList.add(orgId);
        }
        if(!StringUtil.isNullOrEmpty(erpNo)){
            arrayList.add(erpNo);
        }
        sqlBuilder.append(" order by t.create_time desc ");
        return this.findUnique(TDeclareExpertPointVo.class, sqlBuilder.toString(), arrayList.toArray());
    }

    @Override
    public void deleteDeclareExpertPoint(String declareId) {
        Map<String,Object> params = new HashMap<>();
        String sql = " delete from t_declare_expert_point where declare_id = #{declareId,jdbcType=VARCHAR} ";
        params.put("declareId",declareId);
        this.executeSql(sql,params);
    }

    @Override
    public TDeclareExpertPointVo selectTDeclareExpertPointById(String declareId){
        StringBuilder sqlBuilder = new StringBuilder(
                "select t.* FROM t_declare_expert_point t");
        sqlBuilder.append(" where t.declare_id=? ");
        ArrayList<Object> arrayList = new ArrayList<Object>();
        arrayList.add(declareId);
        return this.findUnique(TDeclareExpertPointVo.class, sqlBuilder.toString(), arrayList.toArray());
    }
}
